package com.vhly.tuite.twclient.client;

import com.vhly.tuite.twclient.util.StreamUtil;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 * Created by IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 2010-3-3
 * Time: 15:01:18
 */
public final class TwRequest {
    private int category;
    private int method;
    private int httpMethod;
    private int format;
    private int authType;
    private byte[] authData;
    private Hashtable<String, String> params;
    private int hasID;
    private static final int AUTH_TYPE_NONE = 0;

    public TwRequest(int ca, int md, int hm, int fmt) {
        category = ca;
        method = md;
        httpMethod = hm;
        format = fmt;
        authType = 0;
        authData = null;
        params = new Hashtable<String, String>();
        hasID = 0;
    }

    public void setAPIMethod(int ca, int md, int hid) {
        category = ca;
        method = md;
        hasID = hid;
    }

    public void putParameter(String k, String v) {
        if (k != null && v != null) {
            params.put(k, v);
        }
    }

    public void clearParameters() {
        params.clear();
    }

    public void setAuthType(int atype) {
        this.authType = atype;
    }

    public void setAuthData(byte[] data) {
        this.authData = data;
    }

    public void setFormat(int format) {
        this.format = format;
    }

    public void setHttpMethod(int httpMethod) {
        this.httpMethod = httpMethod;
    }

    public void setHasId(boolean hid) {
        this.hasID = hid ? 1 : 0;
    }

    public void reset() {
        category = -1;
        method = -1;
        httpMethod = -1;
        format = -1;
        authType = 0;
        authData = null;
        params.clear();
    }

    public byte[] genRequestData() {
        byte[] buf = null;
        ByteArrayOutputStream bout = null;
        DataOutputStream dout = null;
        try {
            bout = new ByteArrayOutputStream();
            dout = new DataOutputStream(bout);
            writeRequest(dout);
            buf = bout.toByteArray();
        } catch (IOException ioe) {

        } finally {
            StreamUtil.close(dout);
            StreamUtil.close(bout);
        }
        return buf;
    }

    private void writeRequest(DataOutputStream dout) throws IOException {
        int ci = ((category & 0x0ff) << 4) | (method & 0x0ff);
        dout.write(ci);
        ci = ((httpMethod & 0x03) << 6) | ((authType & 0x03) << 4) | ((format & 0x03) << 2) | ((hasID & 0x03));
        dout.write(ci);
        ci = params.size();
        dout.write(ci);
        if (ci > 0) {
            Enumeration keys = params.keys();
            String k, v;
            while (keys.hasMoreElements()) {
                k = (String) keys.nextElement();
                v = /*(String)*/ params.get(k);
                dout.writeUTF(k);
                dout.writeUTF(v);
            }
        }
        ci = authType;
        if (ci != AUTH_TYPE_NONE) {
            if (authData != null) {
                ci = authData.length;
                dout.writeShort(ci);
                dout.write(authData);
            } else {
                dout.writeShort(0);
            }
        }
    }

    /**
     *
     */
    public void dealloc() {
        category = -1;
        method = -1;
        httpMethod = -1;
        format = -1;
        authType = -1;
        authData = null;
        params.clear();
        params = null;
    }
}
